package com.xhs.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 时间格式化工具类
 */
public class TimeFormatUtil {
    
    private static final DateTimeFormatter CHINESE_FORMATTER = 
        DateTimeFormatter.ofPattern("yyyy年M月d日HH:mm:ss");

    /**
     * 将LocalDateTime格式化为中文时间字符串
     * 格式：2025年7月22日11:16:43
     */
    public static String formatToChinese(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.format(CHINESE_FORMATTER);
    }

    /**
     * 将小红书时间文本转换为中文格式的时间字符串
     * 支持格式：39分钟前、昨天 22:42、1天前、07-13等
     */
    public static String formatXhsTimeToChinese(String timeText) {
        if (timeText == null || timeText.trim().isEmpty()) {
            return "未知时间";
        }
        
        timeText = timeText.trim();
        LocalDateTime now = LocalDateTime.now();
        
        try {
            // 处理"XX分钟前"
            Pattern minutesPattern = Pattern.compile("(\\d+)分钟前");
            Matcher minutesMatcher = minutesPattern.matcher(timeText);
            if (minutesMatcher.find()) {
                int minutes = Integer.parseInt(minutesMatcher.group(1));
                LocalDateTime postTime = now.minusMinutes(minutes);
                return formatToChinese(postTime);
            }
            
            // 处理"XX小时前"
            Pattern hoursPattern = Pattern.compile("(\\d+)小时前");
            Matcher hoursMatcher = hoursPattern.matcher(timeText);
            if (hoursMatcher.find()) {
                int hours = Integer.parseInt(hoursMatcher.group(1));
                LocalDateTime postTime = now.minusHours(hours);
                return formatToChinese(postTime);
            }
            
            // 处理"XX天前"
            Pattern daysPattern = Pattern.compile("(\\d+)天前");
            Matcher daysMatcher = daysPattern.matcher(timeText);
            if (daysMatcher.find()) {
                int days = Integer.parseInt(daysMatcher.group(1));
                LocalDateTime postTime = now.minusDays(days);
                return formatToChinese(postTime);
            }
            
            // 处理"昨天 HH:mm"
            Pattern yesterdayPattern = Pattern.compile("昨天\\s+(\\d{1,2}):(\\d{2})");
            Matcher yesterdayMatcher = yesterdayPattern.matcher(timeText);
            if (yesterdayMatcher.find()) {
                int hour = Integer.parseInt(yesterdayMatcher.group(1));
                int minute = Integer.parseInt(yesterdayMatcher.group(2));
                LocalDateTime postTime = now.minusDays(1).withHour(hour).withMinute(minute).withSecond(0);
                return formatToChinese(postTime);
            }
            
            // 处理"MM-dd"格式
            Pattern datePattern = Pattern.compile("(\\d{2})-(\\d{2})");
            Matcher dateMatcher = datePattern.matcher(timeText);
            if (dateMatcher.find()) {
                int month = Integer.parseInt(dateMatcher.group(1));
                int day = Integer.parseInt(dateMatcher.group(2));
                // 假设是当年的时间
                LocalDateTime postTime = LocalDateTime.of(now.getYear(), month, day, 12, 0, 0);
                // 如果计算出的时间在未来，则认为是去年的
                if (postTime.isAfter(now)) {
                    postTime = postTime.minusYears(1);
                }
                return formatToChinese(postTime);
            }
            
            // 如果无法解析，返回原始文本
            return timeText;
            
        } catch (Exception e) {
            // 解析失败，返回原始文本
            return timeText;
        }
    }
} 